// Copyright 2021 Guoyoko. All Rights Reserved.

#include "SSWLevelEidtor.h"
#include "SWLevelEditorModule.h"
#include "SWDetailBuilderModule.h"
#include "SWLevelEditorToolBar.h"
#include "Widgets/Docking/SDockTab.h"
#include "Engine/GameEngine.h"
#include "SWPlacementModeModule.h"
#include "Styling/SlateBrush.h"
#include "SlateBasics.h"


static const FName LevelEditorModuleName("SplitWindowsLevelEditor");

void SSWLevelEditor::Construct(const FArguments& InArgs)
{
}

void SSWLevelEditor::Initialize(const TSharedRef<SDockTab>& OwnerTab)
{

	TSharedRef<SWidget> ContentWidget = RestoreContentArea(OwnerTab);

	ChildSlot
	[
		ContentWidget
	];
}

TSharedRef<SWidget> SSWLevelEditor::RestoreContentArea(const TSharedRef<SDockTab>& OwnerTab)
{
	FSWLevelEditorModule& LevelEditorModule = FModuleManager::GetModuleChecked<FSWLevelEditorModule>(LevelEditorModuleName);
	LevelEditorModule.SetLevelEditorTabManager(OwnerTab);

	TSharedPtr<FTabManager> LevelEditorTabManager = LevelEditorModule.GetLevelEditorTabManager();

	// Register Level Editor tab spawners
	{
		LevelEditorTabManager->RegisterTabSpawner(FSWLevelEditorTabIds::ToolBar, FOnSpawnTab::CreateRaw(this, &SSWLevelEditor::SpawnLevelEditorTab, FSWLevelEditorTabIds::ToolBar));
		LevelEditorTabManager->RegisterTabSpawner(FSWLevelEditorTabIds::Viewport, FOnSpawnTab::CreateRaw(this, &SSWLevelEditor::SpawnLevelEditorTab, FSWLevelEditorTabIds::Viewport));
		LevelEditorTabManager->RegisterTabSpawner(FSWLevelEditorTabIds::SelectionDetails, FOnSpawnTab::CreateRaw(this, &SSWLevelEditor::SpawnLevelEditorTab, FSWLevelEditorTabIds::SelectionDetails));
		LevelEditorTabManager->RegisterTabSpawner(FSWLevelEditorTabIds::PlaceActorsBrowser, FOnSpawnTab::CreateRaw(this, &SSWLevelEditor::SpawnLevelEditorTab, FSWLevelEditorTabIds::PlaceActorsBrowser));


	}

	TSharedPtr<FTabManager::FLayout> Layout = RestoreLayout();

	TSharedPtr<SWidget> ContentAreaWidget = LevelEditorTabManager->RestoreFrom(Layout.ToSharedRef(),TSharedPtr<SWindow>() );

	return ContentAreaWidget.ToSharedRef();
}

TSharedPtr<FTabManager::FLayout> SSWLevelEditor::RestoreLayout()
{
	const TSharedRef<FTabManager::FLayout> DefaultLayout = FTabManager::NewLayout("MyLevelEditorLayout")
		->AddArea
		(
			FTabManager::NewPrimaryArea()
			->Split
			#pragma region	
			(
				FTabManager::NewSplitter()
				->SetOrientation(EOrientation::Orient_Horizontal)
				->Split
				#pragma region
				(
					FTabManager::NewSplitter()
					->SetOrientation(EOrientation::Orient_Vertical)
					->SetSizeCoefficient(0.6f)
					->Split
					(
						FTabManager::NewStack()
						->SetSizeCoefficient(0.1)
						->AddTab(FSWLevelEditorTabIds::ToolBar, ETabState::OpenedTab)
					)
					->Split
					(
						FTabManager::NewStack()
						->SetSizeCoefficient(0.9)
						->AddTab(FSWLevelEditorTabIds::Viewport, ETabState::OpenedTab)
					)
				)
				#pragma endregion
				->Split
				#pragma region	
				(
					FTabManager::NewSplitter()
					->SetOrientation(EOrientation::Orient_Horizontal)
					->SetSizeCoefficient(0.4f)
					->Split
					(
						FTabManager::NewStack()
						->SetSizeCoefficient(0.5)
						->AddTab(FSWLevelEditorTabIds::PlaceActorsBrowser, ETabState::OpenedTab)
					)
					->Split
					(
						FTabManager::NewStack()
						->SetSizeCoefficient(0.5)
						->AddTab(FSWLevelEditorTabIds::SelectionDetails, ETabState::OpenedTab)
					)
				)
				#pragma endregion
			)
			#pragma endregion
		);
	return DefaultLayout;

}

TSharedRef<SDockTab> SSWLevelEditor::SpawnLevelEditorTab(const FSpawnTabArgs& Args, FName TabIdentifier)
{
	
	if (TabIdentifier == FSWLevelEditorTabIds::ToolBar)
	{
		FSWLevelEditorModule& LevelEditorModule = FModuleManager::GetModuleChecked<FSWLevelEditorModule>(LevelEditorModuleName);
		const TSharedRef<FUICommandList> LevelEditorCommands = LevelEditorModule.GetLevelEditorCommandActionsList();
		
		return SNew(SDockTab)
			.Label(NSLOCTEXT("LevelEditor", "ToolBarTabTitle", "Toolbar"))
			.ShouldAutosize(true)
			[
				FSWLevelEditorToolBar::MakeLevelEditorToolBar(LevelEditorCommands)
			];

	}
	else if (TabIdentifier == FSWLevelEditorTabIds::Viewport)
	{
		TSharedRef<SDockTab> DockTab = SNew(SDockTab)
			.TabRole(ETabRole::NomadTab)
			.OnCanCloseTab_Lambda([]() {return false; })
			.Label(NSLOCTEXT("DigitalTwinsEditor", "Viewport", "Viewport"))
			;

		DockTab->SetContent(GEngine->GetGameViewportWidget().ToSharedRef());

		// Set Viewport tab as main tab
		FSWLevelEditorModule& LevelEditorModule = FModuleManager::GetModuleChecked<FSWLevelEditorModule>(LevelEditorModuleName);
		TSharedPtr<FTabManager> LevelEditorTabManager = LevelEditorModule.GetLevelEditorTabManager();
		LevelEditorTabManager->SetMainTab(DockTab);

		return DockTab;
	}	
	else if (TabIdentifier == FSWLevelEditorTabIds::PlaceActorsBrowser)
	{
		FSWPlacementModeModule& PlacementModeModule = FModuleManager::Get().GetModuleChecked<FSWPlacementModeModule>("SWPlacementMode");

		TSharedRef<SDockTab> DockTab = SNew(SDockTab)
			.Label(NSLOCTEXT("PlacementPanel", "Placement", "Placement"))
			//.ShouldAutosize(true);
			;

		DockTab->SetContent(PlacementModeModule.BuildPropertyPanel().ToSharedRef());

		return DockTab;

	}	
	else if (TabIdentifier == FSWLevelEditorTabIds::SelectionDetails)
	{
		FSWDetailBuilderModule& DetailBuilderModule = FModuleManager::Get().GetModuleChecked<FSWDetailBuilderModule>("SWDetailBuilder");
		
		TSharedRef<SDockTab> DockTab = SNew(SDockTab)
			.Label(NSLOCTEXT("PropertyPanel", "Details", "Details"))
			//.ShouldAutosize(true);
			;
		
		DockTab->SetContent(DetailBuilderModule.BuildPropertyPanel().ToSharedRef());

		return DockTab;
	}

	return SNew(SDockTab);
}

